<template>
  <div style="height: 100vh; display: flex; align-items: center; justify-content: center;">
    <!--顶部横栏-->
    <div style="position: fixed; background-color: #c41335; width: 100%; height: 80px; top: 0; padding-left: 25px;">
      <div style="display: flex; align-items: center; height: 100%; width: 330px; cursor: pointer;"
           @click="$router.push('/')">
        <img src="@/assets/logo.png" alt="" height="80">
        <span style="margin-left: 20px; color: white; font-size: 25px;">小区消防检查系统</span>
      </div>
    </div>

    <div style="display: flex; background-color: white; max-width: 800px; width: 100%;  position: relative; top: 80px; margin: 0 20px;">
      <div v-if="!isMobile" style="flex: 1;">
        <img src="@/assets/register.png" alt="" style="width: 100%; height: 400px; object-fit: contain;">
      </div>
      <div style="flex: 1; display: flex; align-items: center; justify-content: center; box-shadow: 0 0 80px 0 #fac7c7; padding: 10px 0;">
        <el-form :model="user" :rules="rules" ref="loginRef" @submit.native.prevent style="width: 75%">
          <div style="font-size: 20px; font-weight: bold; text-align: center; margin-bottom: 20px">
            欢迎登录
          </div>
          <el-form-item prop="userName">
            <el-input prefix-icon="el-icon-user" placeholder="请输入用户名" v-model="user.userName"
                      ref="begin"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" show-password placeholder="请输入密码"
                      v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item prop="code">
            <div style="display: flex;">
              <el-input prefix-icon="el-icon-circle-check" placeholder="请输入验证码" style="flex: 1"
                        v-model="user.code"></el-input>
              <div style="flex: 1; height: 40px;">
                <valid-code ref="validCodeRef" @update:value="getCode"></valid-code>
              </div>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="success" native-type="submit" @click="login" style="width: 100%">登 录
            </el-button>
          </el-form-item>
          <div style="display: flex; justify-content: space-between;">
            <div>还没有注册？请 <span style="color: blue; cursor: pointer;"
                                     @click="$router.push('/register')">注册</span></div>
            <div style="cursor: pointer; color: blue;" @click="resetDialog=true">忘记密码</div>
          </div>
        </el-form>
      </div>
    </div>

    <!-- 忘记密码弹窗-->
    <el-dialog title="重置密码" :visible.sync="resetDialog" width="30%"
               :before-close="handleClose">
      <el-form :model="resetForm" :rules="resetRules" ref="resetRef" label-width="20%" @submit.native.prevent>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="resetForm.userName"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="resetForm.phone"></el-input>
        </el-form-item>
        <el-button style="visibility: hidden" native-type="submit" @click="resetPassword"></el-button>
      </el-form>
      <span slot="footer">
                <el-button @click="handleClose">取 消</el-button>
                <el-button type="primary" @click="resetPassword">确 定</el-button>
            </span>
    </el-dialog>

  </div>
</template>

<script>
import ValidCode from "@/components/ValidCode.vue";
import {mobileWidth} from "@/utils/mobileWidth";
// import initSSE from "@/utils/sse";

export default {
  name: 'LoginView',
  components: {
    ValidCode
  },
  data() {
    // 验证码校验规则
    var validateCode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入验证码'))
      } else if (value.toLowerCase() !== this.code.toLowerCase()) {
        callback(new Error('验证码错误'))
      } else {
        callback()
      }
    }

    return {
      // 是否开启移动端适配
      isMobile: false,
      code: '',//验证码正确答案
      user: {
        userName: '',
        password: '',
        code: '',//用户输入的验证码
      },
      rules: {
        userName: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          // {min:6,max:18,message:'密码长度为6到18位',trigger:'blur'}
        ],
        code: [
          {validator: validateCode, trigger: 'blur'}
        ]
      },
      resetDialog: false,
      resetForm: {
        userName: '',
        phone: '',
      },
      resetRules: {
        userName: [{required: true, message: '请填写用户名', trigger: 'blur'}],
        phone: [{required: true, message: '请填写手机号', trigger: 'blur'}]
      },
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs['begin'].focus()
    })
    this.isMobile = window.innerWidth < mobileWidth
    // 监听窗口大小变化
    window.addEventListener('resize', () => {
      this.isMobile = window.innerWidth < mobileWidth
    })
  },
  created() {
  },
  methods: {
    getCode(code) {
      this.code = code;
      // console.log(code)
    },
    login() {
      this.$refs['loginRef'].validate((valid) => {
        if (valid) {
          this.$request.post('/user/user/login', this.user).then(res => {
            if (res.code === 200) {
              //登录成功保存用户信息，跳转首页
              localStorage.setItem('user', JSON.stringify(res.data))
              this.$message.success('登录成功')
              this.$router.push('/home')
              //初始化SSE对象
              // initSSE()
            } else {
              this.$refs.validCodeRef.refreshCode()
              this.$message.error(res.msg)
            }
          })
        } else {
          // 表单验证失败刷新验证码
          this.$refs.validCodeRef.refreshCode()
        }
      })
    },
    resetPassword() {
      this.$refs.resetRef.validate(valid => {
        if (valid) {
          this.$request.put('/user/user/resetPassword', this.resetForm).then(res => {
            if (res.code === 200)
              this.$message.success('重置成功，新密码为123')
            else this.$message.error(res.msg)
          })
          this.handleClose()
        }
      })
    },
    handleClose() {
      this.resetDialog = false
      this.$refs.resetRef.resetFields()
    }
  }
}
</script>

<style scoped>

</style>
